<template>
  <div class="fill-in-task-container">
    <CPageTable @register="registerPage" v-model:searchForm="searchForm">
      <!-- <template #headerRightButton>
        <el-button type="primary" @click="handleRouterToSet">
          <CIcon class="g-mr4" type="c-xian-peizhilie"></CIcon>
          填报配置</el-button
        >
      </template> -->
      <template #taskNameSlot="{ scope }">
        <CTableColumn :main-text="scope.row.taskName" :sub-text="scope.row.taskNo"></CTableColumn>
      </template>
      <template #statusSlot="{ scope }">
        <el-tag :type="renderTag(scope.row.status)">{{ $t18(`eci.${scope.row.status}`) }}</el-tag>
      </template>
      <template #op="{ scope }">
        <CTableButtonGroup :buttons-config="getTableButtonsConfig(scope.row)" />
      </template>
    </CPageTable>
  </div>
</template>
<script lang="ts">
export default {
  name: 'FillInTask'
};
</script>
<script lang="ts" setup name="FillInTask">
import { reactive } from 'vue';
import { CPageTable, useCPageTable, CPageTableProps } from '@/components/CPageTable';
import { useFillInTask } from './useFillInTask';
import type { GetAdTaskListResult } from '@/apis/eci/dataManagement/model/fillInTaskModel';
import { FillInTaskStatusEnum, TodoTaskStatusEnum } from '@/constants/enum';
import { CTableColumn, CTableButtonGroup } from '@/components/CTable';
import type { TableButtonItem } from '@/components/CTable';
import { useRouter } from 'vue-router';
import { useKeepAliveRefresh } from '@/hooks/useKeepAliveRefresh';
import { hasBtnPermission } from '@/directives/btnPermission';
import { useMessage } from '@/hooks/useMessage';

const router = useRouter();
const { searchOptions, searchForm, tableOptions, tableFieldsSetting } = useFillInTask();
const { createMessage } = useMessage();
const options = reactive<CPageTableProps>({
  title: '活动数据填报',
  searchOptions: searchOptions,
  tableOptions,
  tableFieldsSetting,
  enableFieldsSetting: true
});
const [registerPage, { refresh }] = useCPageTable(options);
useKeepAliveRefresh(refresh);
const renderTag = (status: string) => {
  switch (status) {
    case FillInTaskStatusEnum.PASS:
      return 'success';
    case FillInTaskStatusEnum.REJECT:
      return 'danger';
    case FillInTaskStatusEnum.FILLING:
      return '';
    case FillInTaskStatusEnum.WAIT_FILL:
      return 'info';
    case FillInTaskStatusEnum.WAIT_AUDIT:
      return 'warning';
    default:
      return 'info';
  }
};
const goDetail = (row: GetAdTaskListResult) => {
  let type = TodoTaskStatusEnum.PROCESSED;
  switch (row.status) {
    case FillInTaskStatusEnum.PASS:
      type = TodoTaskStatusEnum.PROCESSED;
      break;
    case FillInTaskStatusEnum.REJECT:
    case FillInTaskStatusEnum.FILLING:
    case FillInTaskStatusEnum.WAIT_FILL:
    case FillInTaskStatusEnum.WAIT_AUDIT:
      type = TodoTaskStatusEnum.PENDING;
      break;
    default:
      type = TodoTaskStatusEnum.PROCESSED;
      break;
  }
  router.push(`/dataManagement/fillInTask/detail?id=${row.processInstanceId}&type=${type}`);
};

/**
 * 配置表格的按钮
 * @param row 表格行数据
 */
function getTableButtonsConfig(row: GetAdTaskListResult): TableButtonItem[] {
  const text = getOpBtnText(row.status);
  if (!text) {
    return [];
  }
  return [
    {
      text: text,
      onClick: () => checkGoDetail(row)
    }
  ];
}
function checkGoDetail(row: GetAdTaskListResult) {
  switch (row.status) {
    case FillInTaskStatusEnum.PASS:
      if (hasBtnPermission('ECI_FILL_TASK_DATA_VIEW')) {
        goDetail(row);
      } else {
        createMessage.warning('当前您没有权限');
      }
      break;
    case FillInTaskStatusEnum.WAIT_AUDIT:
      if (hasBtnPermission('ECI_FILL_TASK_DATA_AUDIT')) {
        goDetail(row);
      } else {
        createMessage.warning('当前您没有权限');
      }
      break;
    case FillInTaskStatusEnum.REJECT:
    case FillInTaskStatusEnum.FILLING:
    case FillInTaskStatusEnum.WAIT_FILL:
      if (hasBtnPermission('ECI_FILL_TASK_DATA_FILL')) {
        goDetail(row);
      } else {
        createMessage.warning('当前您没有权限');
      }
      break;
    default:
      goDetail(row);
  }
}

/**
 * 获取按钮文案
 */
function getOpBtnText(status: keyof typeof FillInTaskStatusEnum) {
  switch (status) {
    case FillInTaskStatusEnum.PASS:
      return '查看';
    case FillInTaskStatusEnum.WAIT_AUDIT:
      return '审核';
    case FillInTaskStatusEnum.REJECT:
    case FillInTaskStatusEnum.FILLING:
    case FillInTaskStatusEnum.WAIT_FILL:
      return '立即填报';
    default:
      return '';
  }
}

// function handleRouterToSet() {
//   router.push('/dataManagement/fillInConfig');
// }
</script>
